package com.phapli.versionchecking.scheduler;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.phapli.versionchecking.MainActivity;
import com.phapli.versionchecking.R;
import com.phapli.versionchecking.config.Config;
import com.phapli.versionchecking.object.Item;
import com.phapli.versionchecking.preference.MyPrefs;

import java.util.ArrayList;


public class SchedulingService extends IntentService {
    public SchedulingService() {
        super("SchedulingService");
    }

    public static final String TAG = "Scheduling Demo";
    // An ID used to post the notification.

    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    @Override
    protected void onHandleIntent(Intent intent) {
        MyPrefs myPrefs = new MyPrefs(this);
        Item[] items = Config.initItems();
        ArrayList<Item> result = new ArrayList<>();
        for (int i = 0; i < items.length; i++) {
            if (i == 0) {
                items[i].version = Config.getVersionName(this, null);
            } else {
                items[i].version = items[i].parser.parseVersionFromWeb(items[i].url);
                if (!items[i].version.equals("") && !items[i].version.equals(items[0].version)) {
                    if(!myPrefs.getBooleanPref(items[i].id + " notificated")){
                        sendNotification(items[i]);
                    }else {
                        if(!items[i].version.equals(myPrefs.getStringPref(items[i].id + ""))){
                            myPrefs.updateBooleanPref(items[i].id + " notificated", false);
                            sendNotification(items[i]);
                        }
                    }
                    myPrefs.updateStringPref(items[i].id + "", items[i].version);
                }

            }


            result.add(items[i]);
        }

        AlarmReceiver.completeWakefulIntent(intent);
    }

    // Post a notification indicating whether a doodle was found.
    private void sendNotification(Item item) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(Config.intent_name, item.id);
        intent.putExtra(Config.version, item.version);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.update_icon)
                        .setContentTitle(getString(R.string.notification_title))
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(item.version))
                        .setContentText(item.version);
        mBuilder.setContentIntent(contentIntent);
        Notification notification_builder = mBuilder.build();
        notification_builder.flags += Notification.FLAG_ONGOING_EVENT;
        notification_builder.flags += Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(item.id, notification_builder);
    }

}
